package com.wuze.cloud.adapter;

import com.wuze.cloud.dao.SysStorageDao;
import com.wuze.cloud.pojo.SysStorageDO;
import com.wuze.cloud.pojo.SysUserDO;
import com.wuze.cloud.request.SysCreateUserQuery;
import com.wuze.cloud.response.SysUserInfoVO;
import com.wuze.cloud.response.SysUserResponse;
import com.wuze.cloud.response.SysUserVO;
import com.wuze.cloud.util.Md5Util;
import com.wuze.cloud.util.SnowFlakeIdWorker;
import com.wuze.cloud.util.UuidUtil;
import io.jsonwebtoken.Claims;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.wuze.cloud.enums.SysUserStateEnum.UN_CENCEL;

/**
 * @author: wuze
 * @description:
 * @date: 2023/02/16
 */
@Component
public class SysUserAdapter {

    @Autowired
    private SysStorageDao storageDao;

    public SysUserDO buildSysUserDO(SysCreateUserQuery query) {
        SysUserDO user = new SysUserDO();
        BeanUtils.copyProperties(query, user);
        user.setId(SnowFlakeIdWorker.nextId());
        String salt = UuidUtil.getUuid();
        user.setSalt(salt);
        user.setPassword(Md5Util.getMD5Pwd(query.getPassword(), salt));
        user.setState(UN_CENCEL.name());
        return user;
    }

    public SysUserDO buildSysUserDO(Claims claims) {
        if(Objects.isNull(claims)){
            return null;
        }
        SysUserDO user = new SysUserDO();
        user.setId(claims.get("id",Long.class));
        user.setUserName(claims.get("userName",String.class));
        user.setPassword(claims.get("password",String.class));
        user.setRole(claims.get("role",Integer.class));
        user.setRootId(claims.get("rootId", Long.class));
        user.setGender(claims.get("gender", Integer.class));
        user.setPhone(claims.get("phone", String.class));
        user.setSalt(claims.get("salt",String.class));
        user.setCreateTime(claims.get("createTime", Date.class));
        user.setUpdateTime(claims.get("updateTime", Date.class));
        return user;
    }

    public SysUserResponse buildSysUserResponses(SysUserDO user, String token){
        SysUserResponse responses = new SysUserResponse();
        SysUserVO vo = new SysUserVO();
        BeanUtils.copyProperties(user,vo);
        responses.setUserVO(vo);
        responses.setToken(token);
        return responses;
    }

    public SysUserVO buildSysUserVO(SysUserDO user){
        SysUserVO vo = new SysUserVO();
        BeanUtils.copyProperties(user,vo);
        return vo;
    }

    public List<SysUserVO> buildSysUserVOList(List<SysUserDO> users){
        List<SysUserVO> vos = Lists.newArrayList();
        if(CollectionUtils.isEmpty(users)){
            return vos;
        }
        users.forEach(user->{
            vos.add(buildSysUserVO(user));
        });
        return vos;
    }

    public List<SysUserInfoVO> buildSysUserInfoVOList(List<SysUserDO> users){
        List<SysUserInfoVO> vos = Lists.newArrayList();
        if(CollectionUtils.isEmpty(users)){
            return vos;
        }
        users.forEach(user->{
            vos.add(buildSysUserInfoVO(user));
        });
        return vos;
    }

    public SysUserInfoVO buildSysUserInfoVO(SysUserDO user){
        SysUserInfoVO vo = new SysUserInfoVO();
        BeanUtils.copyProperties(user,vo);
        SysStorageDO storage = storageDao.findByUserId(user.getId());
        vo.setCurrSize(storage.getCurrSize());
        vo.setMaxSize(storage.getMaxSize());
        return vo;
    }


}
